package com.hdw.job.utils;

import com.hdw.common.base.BaseException;
import com.hdw.common.constant.EtlConstant;
import com.hdw.common.util.LocalDateUtil;
import com.hdw.common.vo.TableVO;
import com.hdw.med.bean.model.BaseDbTpcomp;
import org.springframework.batch.core.JobParametersBuilder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by liujunlei on 2021/5/25.
 */
public class JobUtil {
    /**
     * 只适用于单表逻辑，请将主表放from后
     * 提取fields字段逻辑为从select至from之间的字符串，逗号分割
     * 提取表名的逻辑为紧挨from后的字符串
     * @param sql
     */
    public static TableVO findTbsAndFldsFromSql(String sql) {
        sql = sql.toUpperCase();
        int firstSelect = sql.indexOf("SELECT");
        int firstFrom = sql.indexOf("FROM");
        String fields = sql.substring(firstSelect+6,firstFrom);
        sql = sql.substring(firstFrom+4).trim();
        String tableName;
        if(sql.indexOf(" ")>0) {
            tableName = sql.substring(0,sql.indexOf(" "));
        } else {
            tableName = sql.substring(0,sql.length());
        }
        TableVO tableVO = new TableVO();
        tableVO.setFields(fields.split(","));
        tableVO.setTableName(tableName);
        return tableVO;
    }

    public static void main(String argus[]) {
        findTbsAndFldsFromSql(null);
    }


    public static String getInsertSqlByMap(Map<String,Object> data,String tableName) {
        String sql ="insert into "+tableName+"(";
        String values = " values (";
        for(String field:data.keySet()) {
            sql+=(field+",");
            values += (":"+field+",");
        }
        sql = sql.substring(0,sql.length()-1)+")";
        values = values.substring(0,values.length()-1)+")";
        return sql+values;
    }

    public static String extrCharFromString(String str,String begin,String end) {
        int b = str.indexOf(begin);
        int e = str.indexOf(end);
        if(b>0 && e>0) {
            return str.substring(b+begin.length(),e);
        }
        return null;
    }

    /**
     * 根据标准类型获取对应数据库数据类型
     * @param dbTpcompList
     * @param euType
     * @param numLength
     * @param numPrec
     * @return
     */
    public static String getDbTypeByStandType(List<BaseDbTpcomp> dbTpcompList,Integer euType,Integer numLength,Integer numPrec) {
        // base_db_tpcomp 修改 取消精度，老代码注释
        /*for (BaseDbTpcomp comp : dbTpcompList) {
            if (euType.equals(comp.getEuJavatp())) {
                if (numPrec > 0 && comp.getFgDeci() > 0) { //小数
                    if (numLength < comp.getNumLength()) {
                        return comp.getEuFldtp() + "(" +numLength+ "," + numPrec + ")";
                    }
                } else if (numPrec == 0 && comp.getFgDeci() == 0) {
                    if (numLength < comp.getNumLength()) {
                        return comp.getEuFldtp() + "(" +numLength+")";
                    }
                }
            }
        }*/
        return null;
    }
}
